package com.thomas.sdk.manager;

import java.util.UUID;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * 获取设备信息的工具类
 * 
 * @author thomas
 * 
 */
public class DeviceManager {
	/**
	 * 获取屏幕的宽度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenWidth(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		return dm.widthPixels;
	}

	/**
	 * sp或者 dp 装换为 px
	 */
	public static int dpToPx(Context context, int dpValue) {
		float scale = context.getResources().getDisplayMetrics().density;
		return Math.round(dpValue * scale);
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	/**
	 * 获取设备的MAC地址
	 * 
	 * @param context
	 * @return
	 */
	public static String getLocalMacAddress(Context context) {

		WifiManager wifi = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		return info.getMacAddress();

	}

	/**
	 * 获取设备的UUID
	 * 
	 * @param context
	 * @return
	 */
	public static String getMyUUID(Context context) {

		final TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);

		final String tmDevice, tmSerial, androidId;

		tmDevice = "" + tm.getDeviceId();

		tmSerial = "" + tm.getSimSerialNumber();

		androidId = ""
				+ android.provider.Settings.Secure.getString(
						context.getContentResolver(),
						android.provider.Settings.Secure.ANDROID_ID);

		UUID deviceUuid = new UUID(androidId.hashCode(),
				((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());

		String uniqueId = deviceUuid.toString();

		return "android_" + uniqueId;

	}

	/**
	 * 
	 * 隐藏软键盘
	 */

	public static void hideSoftInputMode(Context context, View windowToken) {

		InputMethodManager imm = ((InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE));

		imm.hideSoftInputFromWindow(windowToken.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);

	}

	/**
	 * 
	 * 弹出软键盘
	 */

	public static void showSoftInputMode(Context context, View windowToken) {

		final InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);

		imm.showSoftInput(windowToken, InputMethodManager.SHOW_FORCED);

	}

	/**
	 * Check whether the current SDCard is writable.
	 * 
	 * A lack of permission {@link Manifest.permission#WRITE_EXTERNAL_STORAGE}
	 * or a not mounted SDCard will return false.
	 */
	public static boolean isSdcardWritable(Context context) {
		if (!checkPermission(context,
				Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
			return false;
		}
		String state = Environment.getExternalStorageState();
		return Environment.MEDIA_MOUNTED.equals(state);
	}

	/**
	 * 判断SD card 是否可读
	 */
	public static boolean isSdcardReadable(Context context) {
		if (Build.VERSION.SDK_INT >= 19) {
			// 如果是KitKat，先检查是否有read权限，如果没有，则直接返回false
			if (!checkPermission(context,
					"android.permission.READ_EXTERNAL_STORAGE")) {
				return false;
			}
		}
		String state = Environment.getExternalStorageState();
		return Environment.MEDIA_MOUNTED.equals(state);
	}

	/**
	 * Check whether the specified permission is granted to the current package.
	 * 
	 * @param context
	 * @param permissionName
	 *            The permission.
	 * @return True if granted, false otherwise.
	 */
	public static boolean checkPermission(Context context, String permissionName) {
		PackageManager packageManager = context.getPackageManager();
		String pkgName = context.getPackageName();
		return packageManager.checkPermission(permissionName, pkgName) == PackageManager.PERMISSION_GRANTED;
	}
}
